using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using FormsGenerator.Xml;

namespace FormsGenerator
{
    public partial class Preview : System.Web.UI.Page
    {
        protected XmlDocument formXmlDocument;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["id"] == null || Request["id"].Equals(""))
            {
                //Use Server.Transfer to support Reverse proxy. Do not use Response.Redirect!
                Server.Transfer("default.aspx");
            }
            else
            {
                ViewState["GUID"] = Request["id"].Trim();
            }
            
            FormsGeneratorRenderFormPreview.FileUploaded += new FormsGeneratorPlugins.WebControls.FormsGeneratorRenderForm.FileUploadedEventHandler(FormsGeneratorRenderFormPreview_FileUploaded);
            FormsGeneratorRenderFormPreview.FileRemoved += new FormsGeneratorPlugins.WebControls.FormsGeneratorRenderForm.FileRemoveEventHandler(FormsGeneratorRenderFormPreview_FileRemoved);
            if (!IsPostBack)
            {
                try
                {
                    formXmlDocument = XmlHandler.GetForm((string)ViewState["GUID"]);
                    FormsGeneratorRenderFormPreview.FormXml = formXmlDocument.OuterXml;
                    FormsGeneratorRenderFormPreview.DisplayUploadedFileLinks = false;
                    
                    FormsGeneratorRenderFormPreview.DataBind();


                    ArrayList list = new ArrayList();
                    list.Add("Test 1");
                    list.Add("Test 2");
                    list.Add("Test 3");
                    list.Add("Test 4");
                    list.Add("Test 5");

                    //FormsGeneratorRenderFormTest.DataSource = list;
                    //FormsGeneratorRenderFormTest.DataBind();
                }
                catch (Exception ex)
                {
                    LabelErrorMessage.Visible = true;
                    LabelErrorMessage.Text += ex.Message;
                }
                
            }
        }

        void FormsGeneratorRenderFormPreview_FileRemoved(object sender, FormsGeneratorPlugins.WebControls.FileRemoveEventArgs frea)
        {
            LabelErrorMessage.Text += "Removed file " + frea.File.Filename + ".";
            LabelErrorMessage.Visible = true;
        }

        void FormsGeneratorRenderFormPreview_FileUploaded(object sender, FormsGeneratorPlugins.WebControls.FileUploadedEventArgs fuea)
        {
            LabelErrorMessage.Text += "You uploaded the file " + fuea.File.Filename + ". It will not be saved in preview mode.";
            LabelErrorMessage.Visible = true;
        }
    }
}
